﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Orid.Management.Common.Job;
using Orid.Management.Statistics.Tools.Decomposition.Model;
using System.Data.SqlClient;
using System.Data;
using Orid.Management.Statistics.Tools.Decomposition.Logic;
using SqlHelper = Orid.Data.SqlServer.SqlHelper;

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    
    class Order:DataBase
    {
        public virtual ModelOrder GetOrderItem(Guid OrderId)
        {
            return null;
        }

        protected ModelOrder GetOrderItem(Guid OrderId, string connectString, string tableOrderName, string tableMatterName)
        {
            ModelOrder model;
            try
            { 
                if (OrderId == null)
                {
                    return null;
                } 


                StringBuilder sqlText = new StringBuilder();
                sqlText.Append(" SELECT	 [Order].[OrderID],         [Order].[DataCount], [Order].[MatterID]");
                sqlText.Append("        ,[Order].[DeliveryDate],    [Order].[OrderDeliveryDateTime],[Order].[StartDateTime]");
                sqlText.Append("        ,[Order].[FinishDateTime],  [Order].[OrderDateTime],[Order].[CreateDateTime] ");
                sqlText.Append("        ,[Order].[StoreServers] ,   [Order].[UserOrder] ");
                sqlText.Append("        ,[Matter].[MatterName] ,    [Order].[OrderProperty]");
                sqlText.Append("        ,[Order].[OrderCancelDateTime]  ");
                sqlText.Append("  FROM  " + tableOrderName + " [Order]  with(NOLOCK)");//readpast
                sqlText.Append("  INNER JOIN " + tableMatterName + " [Matter]  with(NOLOCK) ON ");//readpast
                sqlText.Append("  [Order].[MatterID] = [Matter].[MatterID] ");
                sqlText.Append("  WHERE [OrderID] = @OrderID");
                sqlText.Append(" ORDER BY [Order].[DeliveryDate] DESC");

                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
                {
                    conn.Open();
                    System.Data.SqlClient.SqlParameter[] SqlParameter;
                    SqlParameter = new System.Data.SqlClient.SqlParameter[]
                                 {     
                                       new System.Data.SqlClient.SqlParameter("@OrderID",System.Data.SqlDbType.UniqueIdentifier), 

                                 };

                    SqlParameter[0].Value = OrderId;

                    SqlDataReader orderReader = SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text, sqlText.ToString(), SqlParameter);
                    model = InitOrderModel(orderReader);
                    if (conn.State == System.Data.ConnectionState.Open)
                        conn.Close();


                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw (new Exception(ex.Message));
            }
            return model;
        }


        protected ModelOrder InitOrderModel(SqlDataReader reader)
        {
            try
            {
                ModelOrder model = new ModelOrder();
                while (reader.Read())
                {
                    model.OrderID = (Guid) reader["OrderID"];

                    if (reader["DataCount"].Equals(DBNull.Value))
                    {
                        model.DataCount = 0;
                    }
                    else
                    {
                        model.DataCount = (int) reader["DataCount"];
                    }

                    model.MatterID = (int) reader["MatterID"];
                    model.MatterName = (string) reader["MatterName"];

                    if (reader["DeliveryDate"].Equals(DBNull.Value))
                    {
                        model.DeliverDate = null;
                    }
                    else
                    {
                        model.DeliverDate = (DateTime) reader["DeliveryDate"];
                    }

                    if (reader["OrderDeliveryDateTime"].Equals(DBNull.Value))
                    {
                        model.OrderDeliverDateTime = null;
                    }
                    else
                    {
                        model.OrderDeliverDateTime = (DateTime) reader["OrderDeliveryDateTime"];
                    }

                    if (reader["StartDateTime"].Equals(DBNull.Value))
                    {
                        model.StarDateTime = null;
                    }
                    else
                    {
                        model.StarDateTime = (DateTime) reader["StartDateTime"];
                    }

                    if (reader["FinishDateTime"].Equals(DBNull.Value))
                    {
                        model.FinishDateTime = null;
                    }
                    else
                    {
                        model.FinishDateTime = (DateTime) reader["FinishDateTime"];
                    }

                    if (reader["OrderDateTime"].Equals(DBNull.Value))
                    {
                        model.OrderDateTime = null;
                    }
                    else
                    {
                        model.OrderDateTime = (DateTime) reader["OrderDateTime"];
                    }
                    if (reader["CreateDateTime"].Equals(DBNull.Value))
                    {
                        model.CreateTime = null;
                    }
                    else
                    {
                        model.CreateTime = (DateTime) reader["CreateDateTime"];
                    }

                    if (!reader["StoreServers"].Equals(DBNull.Value))
                    {
                        string jobSotreID = string.Empty, dataStoreID = string.Empty;

                        string xml = reader["StoreServers"].ToString();

                        System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                        xmlDoc.LoadXml(xml);


                        string rootName = xmlDoc.FirstChild.Name;

                        if (rootName.Equals("Ss", StringComparison.OrdinalIgnoreCase))
                        {
                            jobSotreID = xmlDoc.DocumentElement.SelectSingleNode("./S[@type=1]").InnerText;
                            dataStoreID = xmlDoc.DocumentElement.SelectSingleNode("./S[@type=2]").InnerText;
                        }
                        else if (rootName.Equals("StoreServers", StringComparison.OrdinalIgnoreCase))
                        {

                            jobSotreID = xmlDoc.DocumentElement.SelectSingleNode("./StoreServer[@type=1]").InnerText;
                            dataStoreID = xmlDoc.DocumentElement.SelectSingleNode("./StoreServer[@type=2]").InnerText;
                        }
                        model.JobDBAddress = jobSotreID;
                        model.DataDBAddress = dataStoreID;

                    }

                    //2010-07-02
                    //Add by lidawei
                    if (reader["UserOrder"].Equals(DBNull.Value))
                    {
                        model.UserOrder = null;
                    }
                    else
                    {
                        int userOrder = Convert.ToInt32(reader["UserOrder"]);

                        model.UserOrder = ((Orid.Management.Component.UserOrders) userOrder).ToString();
                    }

                    //2011-6-15
                    //add by lidawei
                    if (reader["OrderProperty"].Equals(DBNull.Value))
                    {
                        model.Orderproperty = null;
                    }
                    else
                    {
                        string orderPerperty = reader["OrderProperty"].ToString();

                        if (!String.IsNullOrEmpty(orderPerperty))
                        {
                            model.Orderproperty = new XmlSerializer<Orid.Management.Component.OrderProperty>().XmlDeserialize(orderPerperty);

                        }
                        

                    }

                    if (reader["OrderCancelDateTime"].Equals(DBNull.Value))
                    {
                        model.OrderCancelDateTime = null;
                    }
                    else
                    {
                        model.OrderCancelDateTime = (DateTime)reader["OrderCancelDateTime"];
                    }
                }
                return model;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                reader.Close();
            }
        }
    }
}
